package com.thunbu.thunbu_meeting_system.activity

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.cathu.lib_commonkt.base.BaseMVVMActivity
import com.google.gson.Gson
import com.thunbu.lib_common.widget.CommonToolBar
import com.thunbu.thunbu_meeting_system.BaseVBQuickAdapter
import com.thunbu.thunbu_meeting_system.bean.StockModel
import com.thunbu.thunbu_meeting_system.bindAdapter
import com.thunbu.thunbu_meeting_system.databinding.ActivityTodoBinding
import com.thunbu.thunbu_meeting_system.databinding.ItemTodoBinding
import com.thunbu.thunbu_meeting_system.gotoActivity
import com.thunbu.thunbu_meeting_system.ternary
import com.thunbu.thunbu_meeting_system.viewMdel.TodoVM

/**
 * @Description: 待办事项
 * @author: Yang.Yang33
 * @Date: 2023/3/21 20:32
 */
class TodoActiviy : BaseMVVMActivity<ActivityTodoBinding,TodoVM>() {
    lateinit var toolBar: CommonToolBar
    private lateinit var mAdapter: BaseVBQuickAdapter<StockModel, ItemTodoBinding>
    var list:MutableList<StockModel> = mutableListOf()
    val ORIGINAL_PAGE = 1
    var mPage = ORIGINAL_PAGE
    var currentIndex = 0

    override fun onMVVMCreated() {
        initToolBar()
        initRv()
        initView()
        initVM()
        loadData()
    }

    private fun initVM() {
        vm.todoPage.observe(this){
            bindViews.refereshLayout.finishLoadMore()
            if (mPage == ORIGINAL_PAGE) {
                mAdapter.setNewData(it.page?.records)
            } else {
                mAdapter.addData(it.page?.records?: mutableListOf())
            }
            if(it.page?.total?:0 > mAdapter.itemCount+1){
                mPage++
            }else{
                bindViews.refereshLayout.setEnableLoadMore(false)
            }
            bindViews.tvTodoCount.text = it.toBeProcessed?.toString()?:"0"
            bindViews.tvFinishCount.text = it.processed?.toString()?:"0"
            bindViews.tvOverCount.text = it.haveFinished?.toString()?:"0"
        }
    }


    private fun initRv() {
        bindViews.rv.layoutManager = LinearLayoutManager(this)
        mAdapter = bindViews.rv.bindAdapter { item, position ->
            tvName.text = item.stockTaskName
            tvTime.text = "发起时间: ${item.createTime}"
            when(item.stockStatus){
                0 ->  tvStatus.text = "未盘点"
                1 ->  tvStatus.text = "盘点中"
                2 ->  tvStatus.text = "已完成盘点"
                3 ->  tvStatus.text = "已终止盘点"
            }
            tvUsername.text = item.creatorName
            tvTodoCount.text = item.toBeCounted.toString()
            tvButtom.visibility = (item.stockStatus == 2 || item.stockStatus == 3).ternary(View.GONE,View.VISIBLE)
            tvButtom.setOnClickListener {
                gotoActivity<InventoryActivity>{
                    putExtra(InventoryActivity.STOCKTASK_CODE, Gson().toJson(item))
                }
            }
            root.setOnClickListener {
                gotoActivity<InventoryActivity>{
                    putExtra(InventoryActivity.STOCKTASK_CODE, Gson().toJson(item))
                    putExtra(InventoryActivity.IS_EDIT, !(item.stockStatus == 2 || item.stockStatus == 3))
                }
            }
        }
    }

    private fun initView() {
        bindViews.refereshLayout.setEnableRefresh(false)
        bindViews.refereshLayout.setEnableLoadMoreWhenContentNotFull(false)
        bindViews.refereshLayout.setEnableLoadMore(true)
        bindViews.refereshLayout.setOnLoadMoreListener {
            loadData()
        }
        bindViews.ll1.setOnClickListener {
            checkLine(0)
        }
        bindViews.ll2.setOnClickListener {
            checkLine(1)
        }
        bindViews.ll3.setOnClickListener {
            checkLine(2)
        }
    }

    private fun reset(){
        mPage = ORIGINAL_PAGE
        bindViews.refereshLayout.setEnableLoadMore(true)
    }

    private fun loadData(){
        vm.requestList(currentIndex,mPage,20)
    }

    private fun checkLine(clickIndex:Int){
        if(currentIndex != clickIndex){
            currentIndex = clickIndex
            reset()
            for (i in 0 until bindViews.lineContainer.childCount){
                bindViews.lineContainer.getChildAt(i).visibility = (currentIndex == i).ternary(View.VISIBLE,View.INVISIBLE)
            }
            loadData()
        }
    }

    private fun initToolBar() {
        toolBar = setToolBar(CommonToolBar::class.java)
        toolBar.setBackAction { finish() }
        toolBar.setTitle("待办事项")
    }
}